package com.tim.util;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 作者：Tim
 * 时间：2021/4/12 17:53
 */
public class DaoUtils {
    //增删改的封装
    public static int commonsUpdate(String sql,Object... args) throws SQLException {
        int count = 0;
        PreparedStatement ps = null;
        try {
            //JDBC 开发步骤
            // 2、获取数据库连接对象
            Connection conn = JDBCUtils.getConn();
            // 3、获取数据库操作对象
            ps = conn.prepareStatement(sql);
            // 3.1、设置占位符的值
            if(args != null){
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i+1,args[i]);
                }
            }
            //4、执行SQL语句
            count =  ps.executeUpdate();
        } finally {
            //6、关闭资源
            JDBCUtils.closeAll(null,ps,null);
        }
        //5、处理返回结果
        return count;
    }

    public static <T> List<T> commonsQuery(Class c, String sql, Object... args) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //2、获取数据库连接对象
            Connection conn = JDBCUtils.getConn();
            //3、获取数据库操作对象
            ps = conn.prepareStatement(sql);
            //3.1、设置占位符的值
            if(args != null){
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i+1,args[i]);
                }
                //4、执行sql语句
                rs = ps.executeQuery();
                List<T> list = new ArrayList<>();
                //5、处理返回结果
                while(rs.next()){
                    try {
                        //通过类对象获取类的属性
                        Field[] fields = c.getDeclaredFields();
                        T obj = (T) c.newInstance();
                        //循环数组给每一个属性赋值
                        for (int i = 0; i < fields.length; i++) {
                            //暴力反射(访问类中的私有成员)
                            fields[i].setAccessible(true);
                            //给当前这个属性进行赋值
                            fields[i].set(obj,rs.getObject(i+1));
                        }
                        list.add(obj);
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
                return list;
            }
        } finally {
            JDBCUtils.closeAll(null,ps,rs);
        }
        return null;
    }
    public static <T> T commonsQueryOne(Class c,String sql,Object... args) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            //2、获取数据库连接对象
            Connection conn = JDBCUtils.getConn();
            //3、获取数据库操作对象
            ps = conn.prepareStatement(sql);
            //3.1、设置占位符的值
            if(args != null){
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i+1,args[i]);
                }
                //4、执行sql语句
                rs = ps.executeQuery();

                //5、处理返回结果
                while(rs.next()){
                    try {
                        //通过类对象获取类的属性
                        Field[] fields = c.getDeclaredFields();
                        T obj = (T) c.newInstance();
                        //循环数组给每一个属性赋值
                        for (int i = 0; i < fields.length; i++) {
                            //暴力反射(访问类中的私有成员)
                            fields[i].setAccessible(true);
                            //给当前这个属性进行赋值
                            fields[i].set(obj,rs.getObject(i+1));
                        }
                        return obj;
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        } finally {
            JDBCUtils.closeAll(null,ps,rs);
        }
        return null;
    }
}
